package selenium.applogic2;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import selenium.applogic.FilmHelper;
import selenium.model.Film;
import selenium.pages.AddMoviePage;
import selenium.pages.ViewFilmPage;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class FilmHelper2 extends DriverBasedHelper implements FilmHelper {

  public FilmHelper2(ApplicationManager2 manager) {
    super(manager.getWebDriver());
  }

  @Override
  public void create(Film film) {
    // TODO Auto-generated method stub

  }

  @Override
  public void delete(Film film) {
    // TODO Auto-generated method stub

  }

  @Override
  public List<Film> search(String title) {
    // TODO Auto-generated method stub
    return null;
  }

  public Film createFilm(Film film) {
      AddMoviePage moviePage = pages.addMoviePage.ensurePageLoaded();

      // Fill form
      moviePage.setNameField(film.getName());
      moviePage.setAkaField(film.getAka());
      moviePage.setYearField(film.getYear());
      moviePage.setDurationField(film.getDuration());
      moviePage.setRatingField(film.getRating());
      moviePage.setFormatsField(film.getFormats());
      moviePage.setOwnNo();
      moviePage.setLoanedYes();
      moviePage.setLoannameField(film.getLoanname());
      moviePage.setCoverField(film.getCover());
      moviePage.setLoandateField(film.getLoandate());
      moviePage.setTrailerField(film.getTrailer());
      moviePage.setNotesField(film.getNotes());
      moviePage.setTaglinesField(film.getTaglines());
      moviePage.setLanguagesField(film.getLanguages());
      moviePage.setPlotoutlineField(film.getPlotoutline());

      // Save form
      moviePage.saveForm();

      return film;
  }

    @Override
    public Boolean addMoviePageLoaded () {
      pages.addMoviePage.ensurePageLoaded();

      return true;
  }

    @Override
    public ViewFilmPage openFilmByName (String filmName) {
        pages.internalMainPage.ensurePageLoaded();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        WebElement filmTitle = driver.findElement(By.xpath("//div[@class=\"title\" and text()=\"" + filmName + "\"]"));
        filmTitle.click();

        return pages.viewFilmPage.ensurePageLoaded();
    }

    @Override
    public void deleteFilm() {
        ViewFilmPage viewFilmPage = pages.viewFilmPage;
        viewFilmPage.clickRemoveButton();

        assertTrue(this.closeAlertAndGetItsText().matches("^Are you sure you want to remove this[\\s\\S]$"));

    }

    @Override
    public void compareFilm(Film film) {
        ViewFilmPage viewFilmPage = pages.viewFilmPage.ensurePageLoaded();

        assertEquals(viewFilmPage.getNameField().getText(), film.getName() + " (" + film.getYear() + ")");
        assertEquals(viewFilmPage.getLoanedField().getText(), "Loaned out to " + film.getLoandate() + " on " + film.getLoanname());
        assertEquals(viewFilmPage.getDurationField().getText(), film.getFormats() + ", " + film.getDuration() + " minutes");
        assertEquals(viewFilmPage.getRatingField().getText(), "Rating: " + film.getRating());
        assertEquals(viewFilmPage.getLanguagesField().getText(), "Languages: " + film.getLanguages().replaceAll("\n", " | "));
        assertEquals(viewFilmPage.getPlotField().getText(), film.getPlotoutline());
        assertEquals(viewFilmPage.getNotesField().getText(), "Personal notes\n" + film.getNotes());
    }

    @Override
    public void checkFilmAbsent(String filmName) {
        pages.internalPage.ensurePageLoaded();
        assertTrue(driver.findElements(By.xpath("//div[@class='title' and text()='" + filmName + "']")).size() == 0);
    }

    @Override
    public int searchFilm(String filmName) {
        pages.addMoviePage
                .ensurePageLoaded()
                .setImdbsearchField(filmName)
                .clickSearchButton();
        return pages.searchResultsPage
                .ensurePageLoaded()
                .getFilmLinksCount(filmName);
    }
}
